<template>
  <div>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        required
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>
<script>
import instance from "../api/api.js";
import debounce from "../utils/debounce";
import {Toast} from "vant";

export default {
  name: "LoginView",
  data() {
    return {
      username: "",
      password: "",
      debouncefn:null,
      next:"",      // 登陆后跳转地址
    };
  },
  methods: {
    onSubmit(item) {
      //   登录验证，用户行为事件
      this.debouncefn();
    //   console.log(item);
    },
    async loginfn() {
      //   登陆验证业务
      let {data} = await instance.post("/login");
    //   console.log(data);
      let token = data.token;
      this.$store.commit("settoken",token);
      let next = this.next;
      Toast({
          type:"success",
          message:"登录成功",
          onClose:()=>{
              this.$router.push({name:next})
          }
      });
    },
  },
  created(){
      this.debouncefn = debounce(this.loginfn,2000);
      this.next = this.$router.currentRoute.params.name;
  }
};
</script>
<style lang="less" scope>
</style>